<?xml version="1.0" encoding="ISO-8859-1"?>

<!--
    Document   : books.xsl
    Created on : September 15, 2013, 11:38 AM
    Author     : doug
    Description:
        Purpose of transformation follows.
-->

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:exslt="http://exslt.org/common"
    version="1.0">
    <xsl:output method="html"/>

    <!-- 
         Transform a book list to a web-page 
    -->
    
    <xsl:template match="/books">
        <html>
            <head>
                <title>books.xsl</title>
            </head>
            <body>
                <xsl:call-template name="title">
                    <xsl:with-param name="desc" select="@description"/>
                    <xsl:with-param name="category" select="@category"/>
                </xsl:call-template>
                <ol>
                    <xsl:apply-templates select="book"/>
                </ol>
                <xsl:variable name="x"/>
            </body>
        </html>
    </xsl:template>

    <xsl:template name="title">
        <xsl:param name="category"/>
        <xsl:param name="desc"/>
        
        <h1>Books</h1>
        <h2>
            <xsl:value-of select="$desc"/> - 
            <xsl:value-of select="$category"/>
        </h2>
    </xsl:template>
    
    <xsl:template match="book">
        <li>
            <p class="centre">
                       <strong class="title">
                            <xsl:value-of select="title"/>
                       </strong>
                <xsl:apply-templates select="subtitle"/>
            </p>
            <p class="centre">By</p>
            <p  class="centre">
                <xsl:apply-templates select="author"/>
            </p>
        </li>
    </xsl:template>
    
    <xsl:template match="subtitle">
        <xsl:variable name="joiner" select="' : '"/>
        <xsl:value-of select="concat($joiner, .)"/>
    </xsl:template>
    
    <xsl:template match="author">
        <xsl:variable name="tokens">
            <xsl:call-template name="tokenize">
                <xsl:with-param name="string" select="." />
            </xsl:call-template>
        </xsl:variable>
        <xsl:call-template name="format-author">
            <xsl:with-param name="tokens"
                select="exslt:node-set($tokens)"/>
        </xsl:call-template>
    </xsl:template>
    
    <xsl:template name="tokenize">
        <xsl:param name="string" />
        <xsl:param name="delimiter" select="' '" />
        <xsl:choose>
            <xsl:when test="$delimiter and contains($string, $delimiter)">
                <token>
                    <xsl:value-of select="substring-before($string, $delimiter)" />
                </token>
                <xsl:text> </xsl:text>
                <xsl:call-template name="tokenize">
                    <xsl:with-param name="string" 
                        select="substring-after($string, $delimiter)" />
                    <xsl:with-param name="delimiter" select="$delimiter" />
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <token><xsl:value-of select="$string" /></token>
                <xsl:text> </xsl:text>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    <xsl:template name="format-author">
        <xsl:param name="tokens"/>
        <strong class="last-name">
            <xsl:value-of select="$tokens/token[last()]"/>
        </strong>
        <xsl:text>, </xsl:text>
        <xsl:for-each select="$tokens/token[position() != last()]">
                <xsl:value-of select="concat(., ' ')"/>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>
